<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="Form" class="demo-ruleForm">

      <el-form-item label="账号" prop="username">
        <el-input style="width: 20%" v-model="ruleForm.username"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input style="width: 20%" v-model="ruleForm.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="login()">登录</el-button>
        <el-button @click="register()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
const axios=require('axios');
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: 'bukong', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs["Form"].validate((valid) => {
        if (valid) {
          axios({
            url: "http://locaLhost:9090/user/login",
            method:"POST",
            data:this.ruleForm,   //this.ruleForm与ruleForm不同
          }).then(resp=>{
            if(!resp.data.username)
            alert("登陆失败！");
            else
            {
              localStorage.setItem("user",JSON.stringify(resp.data));
              this.$router.push("/");
              alert("登陆成功！");
            }
          });
        } else {
          alert("输入错误");
        }
      });
    },
    register() {
    }
  }
}
</script>
<style scoped>
</style>